<?php
//首页显示
namespace Home\Controller;
use Think\Controller;
class SearchController extends CommonController {
    public function index(){
    	
		$keywords = $_GET['keyword'];
		
		if($keywords === iconv('UTF-8', "utf-8", iconv("utf-8", 'UTF-8', $keywords))){
			$keywords=$keywords;
		}else{
			$keywords= iconv("gbk","utf-8",$keywords);
		}
    	if($keywords==null){
    		$this->error('关键词不能为空！');
    	}else{
    		$where['title'] = array('like',"%$keywords%");
	   		$where['ishidden']=0;
	    	$where_lianhe="shop.ishidden=0 and shop.title like '%".$keywords."%'";//联合查询条件
    		//排序
	   		$order=I('get.order',0);
	   		$this->assign('order',$order);
	   		$orders=array(
	   			'0' => 'id desc', 
	   			'1' => 'sale_num desc', 
	   			'2' => 'price desc', 
	   		);
    		$shop=M('shop');
	    	$count      = $shop->where($where)->count();
	    	$this->assign('count',$count);
		    $Page       = new \Think\Page($count,12);// 实例化分页类 传入总记录数
		    $count_page=ceil($count/12);
		    $this->assign('count_page',$count_page);
		    $Page->setConfig('header','条记录');
		    $Page->setConfig('prev','上一页');
		    $Page->setConfig('next','下一页');
		    $show       = $Page->show();// 分页显示输出
	    	$shop_list	= M()
	    	              ->field('shop.*,stroe.title as store_title,stroe.id as store_id')
	    				  ->table(' __SHOP__ AS shop ')
	    				  ->join(' LEFT JOIN __STORE__ AS stroe ON ( stroe.id = shop.store_id ) ')
	    	              ->where($where_lianhe)
	    	              ->order($orders[$order])
	    	              ->limit($Page->firstRow.','.$Page->listRows)
	    	              ->select();
	    
	    	foreach ($shop_list as $key => $value) {
	    		$shop_list[$key]['shop_sku']=json_decode($value['shop_sku'],true);
	    	}
	    	$this->assign('page',$show);// 赋值分页输出
	        $this->assign('shop_list',$shop_list);
	         //销量排行
        	$top_sale=$shop->where('ishidden=0')->order('sale_num desc')->limit(4)->select();
       		$this->assign('top_sale',$top_sale);
       		$this->assign('keywords',$keywords);
    		$this->display();
    	}
    }	
}
?>
